package com.fanben100.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializeUtil {

	/**
	 * 序列化
	 * 
	 * @param name
	 * @param sourceObj
	 * @return
	 */
	public static boolean serialize(String name, Object sourceObj) {
		try {
			ObjectOutputStream out = new ObjectOutputStream(
					new FileOutputStream(name));
			out.writeObject(sourceObj);
			out.close();
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 反序列化
	 * 
	 * @param name
	 * @return
	 */
	public static Object deserialize(String name) {

		ObjectInputStream oin = null;
		Object destObj = null;
		try {
			oin = new ObjectInputStream(new FileInputStream(name));
			destObj = oin.readObject();
			oin.close();
		} catch (FileNotFoundException e) {
			System.out.println(e.getMessage());
		} catch (ClassNotFoundException e) {
			System.out.println(e.getMessage());
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
		return destObj;
	}
}
